package com.sun.dailyprj.view.canvas;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.EmbossMaskFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.sun.dailyprj.R;
import com.sun.dailyprj.util.Logger;


public class ArcProgress extends View{

    private final static float SWEEP_ANGLE = 43;
    private final static float START_ANGLE = 43;
    private final int ARC_PG_COLOR = 0x0CEAEB;
    private final float ARC_PG_RADIUS = 1070;
    private final float ARC_PG_CENTER_X = 0;
    private final float ARC_PG_CENTER_Y = 720;
    private final static boolean sSTYLE = true;//进度样式，默认值 表示为圆弧

    private float mStartAngle = -43;
    private float mSweepAngle = 43;
    private float mRadius = 1070;
    private int mPgColor;
    private float mCircleCenterX;
    private float mCircleCenterY;

    private Paint pgPaint;
    private float barWidth = 10;
    private float progress = 100;
    private int mMaxProgress = 100;

    private CountDownTimer mCountDownTimer;
    private long mTotalTime;
    private long mInterval;
    private float mPercent;
    private float mSweepPercent;
    private boolean mSweepToward = false;//默认由上到下
    private boolean mSweepStyle = sSTYLE;
    private float mPgStroke;
    private int[] mColors;
    private float[] mPosition;
    private BlurMaskFilter mBlur;

    // 向 mask应用一定级别的模糊
    private float blur = 3.5f;
    private int arcradus = 30;

    // 选择要应用的反射等级
    private float specular = 6;
    private EmbossMaskFilter emboss;

    // 设置光源的方向
    private float[] direction = new float[]{1, 1, 1};

    // 设置环境光亮度
    private float light = 0.4f;

    public ArcProgress(Context context) {
        super(context);
        initAttr(context, null);
        initPaint(context);
    }

    public ArcProgress(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttr(context, attrs);
        initPaint(context);
    }

    public ArcProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(context, attrs);
        initPaint(context);

    }

    void initAttr(Context context, AttributeSet attrs) {
        if(attrs == null) return;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.arcpg);
        mSweepAngle = ta.getFloat(R.styleable.arcpg_arc_pg_sweep, SWEEP_ANGLE);
        mStartAngle = ta.getFloat(R.styleable.arcpg_arc_pg_start, START_ANGLE);
        mPgColor = ta.getColor(R.styleable.arcpg_arc_pg_color, ARC_PG_COLOR);
        mCircleCenterX = ta.getFloat(R.styleable.arcpg_arc_pg_c_x, ARC_PG_CENTER_X);
        mCircleCenterY = ta.getFloat(R.styleable.arcpg_arc_pg_c_y, ARC_PG_CENTER_Y);
        mRadius = ta.getFloat(R.styleable.arcpg_arc_pg_radius, ARC_PG_RADIUS);
        mSweepToward = ta.getBoolean(R.styleable.arcpg_arc_pg_towards, false);
        mPgStroke = ta.getFloat(R.styleable.arcpg_arc_pg__width, barWidth);
        mSweepStyle = ta.getBoolean(R.styleable.arcpg_arc_pg_style, sSTYLE);
        Logger.warn("mRadius:" + mRadius + " : " + mCircleCenterY + " : " + mCircleCenterX + " : " + mSweepAngle);
        mRadius = mRadius - mPgStroke;
        mColors = context.getResources().getIntArray(R.array.arcPgColor);
        mPosition = new float[]{0.0f, 0.1f, 0.5f, 1.0f};
    }

    void initPaint(Context context) {
        pgPaint = new Paint();
        pgPaint.setStyle(Paint.Style.STROKE);
        pgPaint.setColor(mPgColor);
        pgPaint.setAntiAlias(true);
        pgPaint.setStrokeWidth(mPgStroke);
        pgPaint.setStrokeCap(Paint.Cap.ROUND);
        pgPaint.setStyle(Paint.Style.STROKE);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(mSweepStyle) {
            //圆心坐标
            float x = mCircleCenterX;
            float y = mCircleCenterY;
            RectF oval = new RectF(x - mRadius, y - mRadius, x + mRadius, y + mRadius);
            pgPaint.setStyle(Paint.Style.STROKE);
            if(progress != 0) {
                mSweepAngle = progress * mSweepPercent;
            } else {
                mSweepAngle = 0;
            }
            Logger.warn("sweepAngle:" + mSweepAngle + "; progress:" + progress);
            SweepGradient sweepGradient = new SweepGradient(mCircleCenterX, mCircleCenterY, mColors, mPosition);
            pgPaint.setShader(sweepGradient);
            if(mSweepToward)
                canvas.drawArc(oval, mStartAngle, mSweepAngle, false, pgPaint);
            else
                canvas.drawArc(oval, mStartAngle, -mSweepAngle, false, pgPaint);
        } else {
            RectF oval = new RectF(mRadius - mPgStroke,0 ,mRadius,canvas.getHeight());
            if(progress != 0) {
                mSweepAngle = progress * mSweepPercent;
            } else {
                mSweepAngle = 0;
            }
            LinearGradient lg = new LinearGradient(0, 0, 100, 100, mColors, mPosition,
                    Shader.TileMode.MIRROR);


            canvas.drawOval(oval,pgPaint);


        }

    }


    public void startProgress(long time, long interval) {
        mTotalTime = time;
        mInterval = interval;
        mPercent = mMaxProgress / (mTotalTime / mInterval);
        if(mCountDownTimer == null) {
            mCountDownTimer = new CountDownTimer(time, interval){
                @Override
                public void onTick(long millisUntilFinished) {
                    progress = mPercent * (millisUntilFinished / mInterval);
                    postInvalidate();
                    if(mArcCallback != null)
                        mArcCallback.onProgress(progress, millisUntilFinished);
                }

                @Override
                public void onFinish() {
                    progress = 0;
                    postInvalidate();
                    if(mArcCallback != null) {
                        mArcCallback.onProgress(progress, 0);
                        mArcCallback.onFinish();
                    }

                }
            }.start();
        }
    }

    public void setMax(int max) {
        mMaxProgress = max;
        mSweepPercent = mSweepAngle / mMaxProgress;
    }

    public void onDestroy() {
        if(mCountDownTimer != null) {
            mCountDownTimer.cancel();
            mCountDownTimer = null;
        }
    }

    public interface ArcCallBack{
        void onProgress(float pg, long time);

        void onFinish();
    }

    private ArcCallBack mArcCallback;

    public void setArcCallBackListener(ArcCallBack cb) {
        mArcCallback = cb;
    }

}
